package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.entity.SalesTop10Report;
import com.sky.enumeration.OrderStatus;
import com.sky.mapper.OrderDetailMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    /**
     * 统计营业额
     * @param begin
     * @param end
     * @return
     */
    @Override
    public TurnoverReportVO getTurnover(LocalDate begin, LocalDate end) {
        ArrayList<LocalDate> localDateList = new ArrayList<>();
        localDateList.add(begin);

        while(!begin.equals(end)) {
            begin = begin.plusDays(1);
            localDateList.add(begin);
        }

        List<Double> amountList = localDateList.stream().map(localDate -> {
            LocalDateTime beginTime = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MAX);

            HashMap<String, Object> map = new HashMap<>();
            map.put("status", OrderStatus.OK.getValue());
            map.put("begin", beginTime);
            map.put("end", endTime);

            Double amount = orderMapper.sumByMap(map);
            if(amount == null)
                amount = 0.0;

            return amount;
        }).collect(Collectors.toList());

        return TurnoverReportVO.builder()
                .dateList(StringUtils.join(localDateList, ","))
                .turnoverList(StringUtils.join(amountList, ","))
                .build();
    }

    /**
     * 统计新增用户和总用户
     * @param begin
     * @param end
     * @return
     */
    @Override
    public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {
        Integer userSum = userMapper.userSum(null, LocalDateTime.of(begin.minusDays(1), LocalTime.MAX));
        if(userSum == null)
            userSum = 0;

        ArrayList<Integer> userSumList = new ArrayList<>();
        List<Integer> newUserSumList = new ArrayList<>();
        ArrayList<LocalDate> localDateList = new ArrayList<>();

        end = end.plusDays(1);
        while(!begin.equals(end)) {
            LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(begin, LocalTime.MAX);

            Integer newUserSum = userMapper.userSum(beginTime, endTime);
            if(newUserSum == null)
                newUserSum = 0;

            localDateList.add(begin);
            userSumList.add(userSum+newUserSum);
            newUserSumList.add(newUserSum);

            begin = begin.plusDays(1);
        }

        return UserReportVO.builder()
                .dateList(StringUtils.join(localDateList, ","))
                .newUserList(StringUtils.join(newUserSumList, ","))
                .totalUserList(StringUtils.join(userSumList, ","))
                .build();
    }

    /**
     * 订单统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public OrderReportVO getOrdersStatistics(LocalDate begin, LocalDate end) {
        Integer orderSum = 0;
        Integer okOrderSum = 0;
        Double rate = 0.0;

        ArrayList<Integer> dateOrderSumList = new ArrayList<>();
        ArrayList<Integer> dateOkOrderSumList = new ArrayList<>();
        ArrayList<LocalDate> dateList = new ArrayList<>();

        end = end.plusDays(1);
        while(!begin.equals(end)) {
            LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(begin, LocalTime.MAX);

            HashMap<String, Object> map = new HashMap<>();
            map.put("begin", beginTime);
            map.put("end", endTime);

            List<Orders> list = orderMapper.listByMap(map);
            int size = list.size();

            List<Orders> okList = list.stream().filter(orders -> orders.getStatus() == OrderStatus.OK.getValue()).collect(Collectors.toList());
            int okSize = okList.size();

            dateList.add(begin);
            dateOrderSumList.add(size);
            dateOkOrderSumList.add(okSize);

            orderSum += size;
            okOrderSum += okSize;

            begin = begin.plusDays(1);
        }


        if(okOrderSum != 0)
            rate = Double.valueOf(okOrderSum)/Double.valueOf(orderSum);

        return OrderReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .orderCountList(StringUtils.join(dateOrderSumList, ","))
                .validOrderCountList(StringUtils.join(dateOkOrderSumList, ","))
                .orderCompletionRate(rate)
                .totalOrderCount(orderSum)
                .validOrderCount(okOrderSum)
                .build();
    }

    /**
     * 商品销量排名前十
     * @param begin
     * @param end
     * @return
     */
    @Override
    public SalesTop10ReportVO getTop10(LocalDate begin, LocalDate end) {
        PageHelper.startPage(0, 10);

        ArrayList<Long> numberList = new ArrayList<>();
        ArrayList<String> nameList = new ArrayList<>();

        end = end.plusDays(1);
        while(!begin.equals(end)) {
            LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(begin, LocalTime.MAX);

            HashMap<String, Object> map = new HashMap<>();
            map.put("begin", beginTime);
            map.put("end", endTime);

            List<SalesTop10Report> all = orderDetailMapper.SalesTop10ReportList(map);

            all.forEach(salesTop10Report -> {
                numberList.add(salesTop10Report.getSum());
                nameList.add(salesTop10Report.getName());
            });

            begin = begin.plusDays(1);
        }

        return SalesTop10ReportVO.builder()
                .nameList(StringUtils.join(nameList, ","))
                .numberList(StringUtils.join(numberList, ","))
                .build();
    }
}
